﻿using System;
using System.IO;
using System.Runtime.Serialization;
using Sfd.Entities;

namespace Sfd.Repositories.Files
{
    public class AzureUserDataContractRepository : IAzureUserRepository
    {
        private readonly string _fileName;

        public AzureUserDataContractRepository(string fileName)
        {
            _fileName = fileName;
        }

        public void Save(AzureUser user)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            using (var stream = new FileStream(_fileName, FileMode.Create, FileAccess.Write))
            {
                var serializer = new DataContractSerializer(typeof (AzureUser));
                serializer.WriteObject(stream, user);
            }
        }

        public AzureUser LoadUser()
        {
            using (var stream = new FileStream(_fileName, FileMode.Open, FileAccess.Read))
            {
                var serializer = new DataContractSerializer(typeof(AzureUser));
                return (AzureUser) serializer.ReadObject(stream);
            }            
        }
    }
}
